package com.anandsoftware.service.impl;

import java.util.Map;

import com.anandsoftware.application.ApplicationContext;
import com.anandsoftware.db.io.DatabaseIO;
import com.anandsoftware.security.SecurityAlgorithm;
import com.anandsoftware.security.SecurityUtil;
import com.anandsoftware.security.impl.BlowFish;
import com.anandsoftware.service.RegistrationService;
import com.anandsoftware.system.domain.SystemInformation;
import com.google.gson.Gson;

public class RegistrationServiceImpl extends BaseServiceImpl implements
		RegistrationService {

	/**
	 * @param key
	 *            key of Encryption Algorithm.
	 * @param systemInformation
	 *            receives all system information to be sent on server.
	 * @return true in case of successful registration on server else false.
	 */

	@Override
	public boolean registerSystemInformation(
			SystemInformation systemInformation, String key, String myID) {
		// TODO Auto-generated method stub
		String jsonSystemInformation = new Gson().toJson(systemInformation);
		BlowFish blowFish = SecurityUtil
				.getInstance(SecurityAlgorithm.BLOWFISH);
		blowFish.setKey(key);
		blowFish.setIv(ApplicationContext.DEFAULT_IV);
		String encryptedJson = blowFish.encrypt(jsonSystemInformation);
		String responce = sendPostRequest(
				ApplicationContext.REQUEST_ACCESS_TOKEN + myID + "&"
						+ ApplicationContext.REGISTERATION_SERVICE_TOKEN
						+ encryptedJson, REGISTRATION_SERVICE_REQUEST_URL);

		if (responce != null && responce.trim().equals("1")) {
			return true;
		}
		return false;
	}

	/**
	 * Not Actually used by server right now.
	 * 
	 * @deprecated will be removed in next version.
	 * @param systemInformation
	 *            receives all system information to be sent on server.
	 * @return true in case of successful registration on server else false.
	 */

	@Override
	public boolean registerSystemInformation(
			SystemInformation systemInformation, String myID) {
		// TODO Auto-generated method stub
		String jsonSystemInformation = new Gson().toJson(systemInformation);
		String responce = sendPostRequest(
				ApplicationContext.REQUEST_ACCESS_TOKEN + myID + "&"
						+ ApplicationContext.REGISTERATION_SERVICE_TOKEN
						+ jsonSystemInformation,
				REGISTRATION_SERVICE_REQUEST_URL);
		if (responce != null && responce.trim().equals("1")) {
			return true;
		}
		return false;
	}

	/**
	 * @param systemInformation
	 *            receives all system information to be saved on local machine.
	 * @return true in case of successful registration on server else false.
	 * @param dbIO
	 *            configured {@link DatabaseIO} object.
	 */
	@Override
	public boolean registerSystemInformationLocal(
			SystemInformation systemInformation, DatabaseIO dbIO) {
		try {
			// TODO Auto-generated method stub
			Map<String, String> map = SecurityUtil.encryptPropertyAndValue(
					systemInformation, SecurityAlgorithm.SHA1);
			dbIO.save(map);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

}
